package cn.cjxy.controller;

import cn.cjxy.domain.Address;
import cn.cjxy.domain.User;
import cn.cjxy.untils.R;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/user")
@RestController//@Controller+@Responsebody
public class UserContrller {
    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    public R<List<User>> getUser() {
        //路径  http://localhost:8081/cjxy/user
        System.out.println("查询所有用户成功！");
        List<User> list = Arrays.asList(
                new User("张三",22,new Address("四川省","成都市")),
                new User("李四",23,new Address("重庆市","巴南区"))

        );


        return R.ok("查询所有用户成功",list);
    }

    /**
     * 查询单个用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<User> getUserById(@PathVariable("id") Integer id) {
        //路径  http://localhost:8081/cjxy/user/3
        System.out.println("查询单个用户成功！");
        User user = new User("张三", 22, new Address("四川省", "成都市"));
        return R.ok("查询单个用户成功",user);
    }

    /**
     * 新增单个用户
     * @param user
     * @return
     */
    @PostMapping
    public String addUser(@RequestBody User user) {
        //路径  http://localhost:8081/cjxy/user
        System.out.println("新增单个用户成功！");
        return "请求成功";
    }

    /**
     * 修改单个用户
     * @param user
     * @return
     */
    @PutMapping
    public String updateUser(@RequestBody User user) {
        System.out.println("修改单个用户成功！");
        return "请求成功";
    }

    /**
     * 删除单个用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R<Void> deleteUser(@PathVariable("id") Integer id) {
        System.out.println("删除单个用户失败！");
        return R.fail();
    }

}
